﻿using System;
using Sumen.Business;
using Sumen.Business.Permission;
using Sumen.Business.Presenter.Actor;
using DevExpress.XtraSplashScreen;
using Sumen.Business.Presenter.Category;
using Sumen.Library.Classes.Common;
using Sumen.Library.Enum;
using Sumen.Library.Froms;
using Sumen.Model.Model;



namespace Sumen.GUI.Entities
{
    public partial class Frm_Customer : XtraUserControlKira
    {
        Frm_Customer_Edit frm_customer_edit;
        private EnumFormCode _fromCode = EnumFormCode.FrmCustomer;
        private CustomerPresenter _customerPresenter;
        private CustomerTypePresenter _customerTypePresenter;

        public Frm_Customer()
        {
            InitializeComponent();
            buttonsArray1.capQuyen(new Permission(_fromCode.ToString(), ClsPublic.User.User_Username));
            _customerPresenter = new CustomerPresenter();
            _customerTypePresenter = new CustomerTypePresenter();
        }

        private void Frm_Customer_Load(object sender, EventArgs e)
        {
            LoadForm();
        }

        public override void LoadForm()
        {
            gridControl1.DataSource = _customerPresenter.GetAll();
            repositoryItemLookUpEdit1.DataSource = _customerTypePresenter.GetAll();
        }

        #region Button Event

        private void buttonsArray1_btnEventAdd_click(object sender, EventArgs e)
        {
            if (frm_customer_edit == null)
            {
                frm_customer_edit = new Frm_Customer_Edit();
                frm_customer_edit.EnumFormStatus = EnumFormStatus.Add;
                frm_customer_edit.ShowDialog();
            }
            else
            {
                frm_customer_edit.EnumFormStatus = EnumFormStatus.Add;
                frm_customer_edit.ShowDialog();
            }
        }

        private void buttonsArray1_btnEventClose_click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void buttonsArray1_btnEventDelete_click(object sender, EventArgs e)
        {
            string sUsername = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, colUsername) + string.Empty;
            {
                if (ClsMsgBox.XacNhanXoaThongTin())
                {
                    if (_customerPresenter.Delete(sUsername) > 0)
                    {
                        ClsMsgBox.ThongTin("Xóa khách hàng thành công");

                        ClsPublic.WriteLog(_fromCode, EnumFormStatus.Add, sUsername, "");
                        Refresh();
                    }
                }
            }
        }

        private void buttonsArray1_btnEventUpdate_click(object sender, EventArgs e)
        {
            string sUsername = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, colUsername) + string.Empty;
            {
                customer cus = _customerPresenter.GetById(sUsername);
                if (cus != null)
                {
                    if (frm_customer_edit == null)
                    {
                        frm_customer_edit = new Frm_Customer_Edit();
                        frm_customer_edit.Customer = cus;
                        frm_customer_edit.EnumFormStatus = EnumFormStatus.Modify;
                        frm_customer_edit.ShowDialog();
                    }
                    else
                    {
                        frm_customer_edit.EnumFormStatus = EnumFormStatus.Modify;
                        frm_customer_edit.Customer = cus;
                        frm_customer_edit.ShowDialog();
                    }
                }
                else
                {
                    ClsMsgBox.Loi("Không tìm thấy khách hàng.");
                }
            }
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            string sUsername = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, colUsername) + string.Empty;
            {
                customer cus = _customerPresenter.GetById(sUsername);
                if (cus != null)
                {
                    if (frm_customer_edit == null)
                    {
                        frm_customer_edit = new Frm_Customer_Edit();
                        frm_customer_edit.Customer = cus;
                        frm_customer_edit.EnumFormStatus = EnumFormStatus.Modify;
                        frm_customer_edit.ShowDialog();
                    }
                    else
                    {
                        frm_customer_edit.EnumFormStatus = EnumFormStatus.Modify;
                        frm_customer_edit.Customer = cus;
                        frm_customer_edit.ShowDialog();
                    }
                }
                else
                {
                    ClsMsgBox.Loi("Không tìm thấy khách hàng.");
                }
            }
        }

        #endregion









    }
}